add_subdirectory(bindings)
set(HAVE_ALL_PYTHON_HEADERS ${HAVE_ALL_PYTHON_HEADERS} PARENT_SCOPE)

if( OPT_BUILD_PACKAGE_DEFAULT )
  set(OPENRAVEPY_EGG "${CMAKE_CURRENT_BINARY_DIR}/openravepy.egg-info")
  configure_file("${CMAKE_CURRENT_SOURCE_DIR}/openravepy.egg-info.in" "${OPENRAVEPY_EGG}" IMMEDIATE @ONLY)
  install(FILES "${OPENRAVEPY_EGG}" DESTINATION ${OPENRAVE_PYTHON_INSTALL_DIR}  COMPONENT openrave-python-minimal)
  
  set(OPENRAVEPY_INIT "${CMAKE_CURRENT_BINARY_DIR}/__init__.py")
  configure_file("${CMAKE_CURRENT_SOURCE_DIR}/__init__.py.in" "${OPENRAVEPY_INIT}" IMMEDIATE @ONLY)
  set(OPENRAVEPY_MAIN "${CMAKE_CURRENT_BINARY_DIR}/__main__.py")
  configure_file("${CMAKE_CURRENT_SOURCE_DIR}/openravepy.__main__.py.in" "${OPENRAVEPY_MAIN}" IMMEDIATE @ONLY)
  set(OPENRAVEPY_ROBOT "${CMAKE_CURRENT_BINARY_DIR}/robot.py")
  configure_file("${CMAKE_CURRENT_SOURCE_DIR}/openravepy.robot.py.in" "${OPENRAVEPY_ROBOT}" IMMEDIATE @ONLY)
  set(OPENRAVEPY_CREATEPLUGIN "${CMAKE_CURRENT_BINARY_DIR}/createplugin.py")
  configure_file("${CMAKE_CURRENT_SOURCE_DIR}/openravepy.createplugin.py.in" "${OPENRAVEPY_CREATEPLUGIN}" IMMEDIATE @ONLY)
  set(CPACK_COMPONENT_OPENRAVE-PYTHON-MINIMAL_DISPLAY_NAME "Python ${PYTHON_MAJORMINOR_VERSION} bindings minimal subset" PARENT_SCOPE)
  set(CPACK_COMPONENT_OPENRAVE-PYTHON-MINIMAL_DESCRIPTION "Uninstall this package to remove all python bindings of all installations" PARENT_SCOPE)
  set(CPACK_COMPONENT_OPENRAVE-PYTHON-MINIMAL_DEPENDS openrave-minimal PARENT_SCOPE)
endif()

file(READ "${CMAKE_CURRENT_SOURCE_DIR}/../src/cppexamples/FindOpenRAVE.cmake" FindOpenRAVE)

# python executables need renaming
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/openrave.py.in" "${CMAKE_CURRENT_BINARY_DIR}/openrave${OPENRAVE_BIN_SUFFIX}.py" IMMEDIATE @ONLY)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/openrave-robot.py.in" "${CMAKE_CURRENT_BINARY_DIR}/openrave${OPENRAVE_BIN_SUFFIX}-robot.py" IMMEDIATE @ONLY)
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/openrave-createplugin.py.in" "${CMAKE_CURRENT_BINARY_DIR}/openrave${OPENRAVE_BIN_SUFFIX}-createplugin.py" IMMEDIATE @ONLY)

install(FILES "${CMAKE_CURRENT_BINARY_DIR}/openrave${OPENRAVE_BIN_SUFFIX}.py" "${CMAKE_CURRENT_BINARY_DIR}/openrave${OPENRAVE_BIN_SUFFIX}-robot.py" "${CMAKE_CURRENT_BINARY_DIR}/openrave${OPENRAVE_BIN_SUFFIX}-createplugin.py" DESTINATION bin PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_EXECUTE WORLD_READ COMPONENT ${COMPONENT_PREFIX}python)

# install rest python files
if (OPT_PYTHON)
  install(FILES ${OPENRAVEPY_INIT} DESTINATION ${OPENRAVEPY2_INSTALL_DIR}  COMPONENT openrave-python-minimal)
  install(FILES metaclass.py openravepy_ext.py misc.py trajectoryutils.py pyANN.py DESTINATION ${OPENRAVEPY2_VER_INSTALL_DIR} COMPONENT ${COMPONENT_PREFIX}python)
  install(FILES openravepy.__init__.py DESTINATION ${OPENRAVEPY2_VER_INSTALL_DIR} COMPONENT ${COMPONENT_PREFIX}python RENAME __init__.py)
  install(FILES ${OPENRAVEPY_MAIN} ${OPENRAVEPY_ROBOT} ${OPENRAVEPY_CREATEPLUGIN} DESTINATION ${OPENRAVEPY2_VER_INSTALL_DIR} COMPONENT ${COMPONENT_PREFIX}python)
  install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/examples" DESTINATION ${OPENRAVEPY2_VER_INSTALL_DIR} FILE_PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_EXECUTE WORLD_READ COMPONENT ${COMPONENT_PREFIX}python PATTERN ".svn" EXCLUDE PATTERN ".pyc" EXCLUDE)
  install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/interfaces" DESTINATION ${OPENRAVEPY2_VER_INSTALL_DIR}  FILE_PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ COMPONENT ${COMPONENT_PREFIX}python PATTERN ".svn" EXCLUDE PATTERN ".pyc" EXCLUDE)
  install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/databases" DESTINATION ${OPENRAVEPY2_VER_INSTALL_DIR}  FILE_PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ COMPONENT ${COMPONENT_PREFIX}python PATTERN ".svn" EXCLUDE PATTERN ".pyc" EXCLUDE)
endif()
if (OPT_PYTHON3)
  install(FILES ${OPENRAVEPY_INIT} DESTINATION ${OPENRAVEPY3_INSTALL_DIR}  COMPONENT openrave-python-minimal)
  install(FILES metaclass.py openravepy_ext.py misc.py trajectoryutils.py pyANN.py DESTINATION ${OPENRAVEPY3_VER_INSTALL_DIR} COMPONENT ${COMPONENT_PREFIX}python)
  install(FILES openravepy.__init__.py DESTINATION ${OPENRAVEPY3_VER_INSTALL_DIR} COMPONENT ${COMPONENT_PREFIX}python RENAME __init__.py)
  install(FILES ${OPENRAVEPY_MAIN} ${OPENRAVEPY_ROBOT} ${OPENRAVEPY_CREATEPLUGIN} DESTINATION ${OPENRAVEPY3_VER_INSTALL_DIR} COMPONENT ${COMPONENT_PREFIX}python)
  install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/examples" DESTINATION ${OPENRAVEPY3_VER_INSTALL_DIR} FILE_PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_EXECUTE WORLD_READ COMPONENT ${COMPONENT_PREFIX}python PATTERN ".svn" EXCLUDE PATTERN ".pyc" EXCLUDE)
  install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/interfaces" DESTINATION ${OPENRAVEPY3_VER_INSTALL_DIR}  FILE_PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ COMPONENT ${COMPONENT_PREFIX}python PATTERN ".svn" EXCLUDE PATTERN ".pyc" EXCLUDE)
  install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/databases" DESTINATION ${OPENRAVEPY3_VER_INSTALL_DIR}  FILE_PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ COMPONENT ${COMPONENT_PREFIX}python PATTERN ".svn" EXCLUDE PATTERN ".pyc" EXCLUDE)
endif()

set(CPACK_COMPONENT_${COMPONENT_PREFIX_UPPER}PYTHON_DISPLAY_NAME "Python ${PYTHON_MAJORMINOR_VERSION} bindings" PARENT_SCOPE)
set(CPACK_COMPONENT_${COMPONENT_PREFIX_UPPER}PYTHON_DEPENDS ${COMPONENT_PREFIX}base python-numpy python-pkg-resources openrave-python-minimal PARENT_SCOPE)
set(CPACK_COMPONENT_${COMPONENT_PREFIX_UPPER}PYTHON_RECOMMENDS python-h5py python-scipy liblapack-dev PARENT_SCOPE)
set(CPACK_COMPONENT_${COMPONENT_PREFIX_UPPER}PYTHON_SUGGESTS ipython python-qt4 PARENT_SCOPE)

if( OPT_BUILD_PACKAGE_DEFAULT AND OPENRAVE_BIN_SUFFIX )
  InstallSymlink(openrave${OPENRAVE_BIN_SUFFIX}.py
                 ${CMAKE_INSTALL_PREFIX}/bin/openrave.py)
  InstallSymlink(openrave${OPENRAVE_BIN_SUFFIX}-robot.py
                 ${CMAKE_INSTALL_PREFIX}/bin/openrave-robot.py)
  InstallSymlink(openrave${OPENRAVE_BIN_SUFFIX}-createplugin.py
                 ${CMAKE_INSTALL_PREFIX}/bin/openrave-createplugin.py)

  if (OPT_PYTHON)
    InstallSymlink(
      ${OPENRAVEPY_VER_NAME}
      ${CMAKE_INSTALL_PREFIX}/${OPENRAVEPY2_INSTALL_DIR}/_openravepy_
    )
  endif()

  if (OPT_PYTHON3)
    InstallSymlink(
      ${OPENRAVEPY_VER_NAME}
      ${CMAKE_INSTALL_PREFIX}/${OPENRAVEPY3_INSTALL_DIR}/_openravepy_
    )
  endif()
endif()

# ikfast component
# install previous versions of ikfast also since don't know which sympy version user will install
if (OPT_PYTHON)
  install(FILES ikfast.py ikfast_sympy0_6.py DESTINATION ${OPENRAVEPY2_VER_INSTALL_DIR} PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_EXECUTE WORLD_READ COMPONENT ${COMPONENT_PREFIX}ikfast)
  install(FILES ikfast.h ikfast_generator_cpp.py ikfast_generator_cpp_sympy0_6.py DESTINATION ${OPENRAVEPY2_VER_INSTALL_DIR} COMPONENT ${COMPONENT_PREFIX}ikfast)
endif()
if (OPT_PYTHON3)
  install(FILES ikfast.py ikfast_sympy0_6.py DESTINATION ${OPENRAVEPY3_VER_INSTALL_DIR} PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_EXECUTE WORLD_READ COMPONENT ${COMPONENT_PREFIX}ikfast)
  install(FILES ikfast.h ikfast_generator_cpp.py ikfast_generator_cpp_sympy0_6.py DESTINATION ${OPENRAVEPY3_VER_INSTALL_DIR} COMPONENT ${COMPONENT_PREFIX}ikfast)
endif()

if( NOT OPENRAVE_USE_LOCAL_SYMPY )
  set(IKFAST_USES python-sympy python-mpmath)
else()
  set(IKFAST_USES)
endif()
if( CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX )
  set(IKFAST_USES ${IKFAST_USES} g++)
endif()
set(CPACK_COMPONENT_${COMPONENT_PREFIX_UPPER}IKFAST_DEPENDS ${COMPONENT_PREFIX}python PARENT_SCOPE)
set(CPACK_COMPONENT_${COMPONENT_PREFIX_UPPER}IKFAST_RECOMMENDS ${IKFAST_USES} PARENT_SCOPE)
set(CPACK_COMPONENT_${COMPONENT_PREFIX_UPPER}IKFAST_DISPLAY_NAME "Robot Kinematics Compiler" PARENT_SCOPE)

set(CPACK_COMPONENTS_ALL ${CPACK_COMPONENTS_ALL} ${COMPONENT_PREFIX}python ${COMPONENT_PREFIX}ikfast PARENT_SCOPE)
